#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <string>

//共享资源，火车票
int ticket = 10000;

void *GetTicket(void *args)
{
   
    string name = static_cast<const char *>(args);
    while (true)
    {
   
        if (ticket > 0)
        {
   
            cout << name << " 正在进行抢票: " << ticket << endl;
            ticket--;
            //以微秒为单位进行休眠，模拟真实的抢票要花费的时间
            usleep(1000);
        }
        else
        {
   
            break;
        }
    }
}

int main()
{
   
    pthread_t t1,t2,t3,t4;
    pthread_create(&t1,nullptr,GetTicket,(void*)"thread->1");
    pthread_create(&t2,nullptr,GetTicket,(void*)"thread->2");
    pthread_create(&t3,nullptr,GetTicket,(void*)"thread->3");
    pthread_create(&t4,nullptr,GetTicket,(void*)"thread->4");

    pthread_join(t1,nullptr);
    pthread_join(t2,nullptr);
    pthread_join(t3,nullptr);
    pthread_join(t4,nullptr);
    return 0;
}

